package com.nature.ffmpegforandroid

import android.content.Context
import android.util.AttributeSet
import android.view.SurfaceView

/**
 *User: RandBII
 *Date: 2020/12/31
 *Description:
 */
class MySurfaceView(context: Context, attrs: AttributeSet? = null) :
    SurfaceView(context, attrs) {

    private var mRatioWidth = 0
    private var mRatioHeight = 0


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val w = MeasureSpec.getSize(widthMeasureSpec)
        val h = MeasureSpec.getSize(heightMeasureSpec)

        if (mRatioHeight == 0 || mRatioWidth == 0) {
            setMeasuredDimension(mRatioWidth, mRatioHeight)
        } else {
            if (w < h * mRatioWidth / mRatioHeight) {
                setMeasuredDimension(w, w * mRatioHeight / mRatioWidth)
            } else {
                setMeasuredDimension(h * mRatioWidth / mRatioHeight, h)
            }
        }
    }

    fun setAspectRatio(width: Int, height: Int) {
        if (width < 0 || height < 0) throw IllegalArgumentException("invalid parameter")
        mRatioHeight = width
        mRatioHeight = height
        requestLayout()
    }

}